/**
 * 程序的退出方式
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <unistd.h>

void atexit_fun_one(void)
{
    printf("Atexit Function First!\n");
}

void atexit_fun_two(void)
{
    printf("Atexit Function Second!\n");
}

void atexit_fun_three(void)
{
    printf("Atexit Function Third!\n");
}

int main(int argc, char **argv)
{
    if (argc < 3)
    {
        fprintf(stderr, "%s <filename> <[return|exit|_exit]>\n", argv[0]);
        exit(0);
    }

    FILE *fp = fopen(argv[1], "w");
    if (fp == NULL)
    {
        perror("fopen error : ");
        exit(0);
    }

    fprintf(fp, "Hello Gino");

    atexit(atexit_fun_one);
    atexit(atexit_fun_two);
    atexit(atexit_fun_three);

    if (strcmp(argv[2], "return") == 0)
    {
        return 0;
    }
    else if (strcmp(argv[2], "exit") == 0)
    {
        exit(EXIT_SUCCESS);
    }
    else if (strcmp(argv[2], "_exit") == 0)
    {
        _exit(EXIT_SUCCESS);
    }
    else
    {
        fprintf(stderr, "%s <filename> <[return|exit|_exit]>\n", argv[0]);
        _exit(EXIT_SUCCESS);
    }


    return 0;
}